#!/bin/bash

echo "#Generating the .ptrace_tmp file. Updated whenever a new pp analysis is performed"
echo "#Author: Min Li, Contact: mli46@wisc.edu"
echo -ne "#Generated Date: "
date
echo 

## power unit: W
echo "total region count is $HSTA_REG_CNT"

## parsing the power report and generate the hotspot needed file --- .ptrace
awk -v region_cnt=$HSTA_REG_CNT '/Power Group/ { getline; for(i=0;i<region_cnt;i++) {getline; print} }' $HSTA_PP_REPORT_FILE | sort -k2 -tn -n > $HSTA_UNIQ_TMP_FILE

## only print out the total power consumption
awk '/region/ {print $1, $5}' $HSTA_UNIQ_TMP_FILE > $HSTA_UNIQ_TMP_FILE"_"

## some regions can have 0 power consumption thus not listed
awk -F 'n' '{print $2}' $HSTA_UNIQ_TMP_FILE"_" | awk -F ' ' '{print $1}' > $HSTA_UNIQ_TMP_FILE
min_power=$(awk -F ' ' 'BEGIN{a=1000000} {if (a>$2) a=$2} END{print a}' $HSTA_UNIQ_TMP_FILE"_")
echo $min_power

awk 'function f(a){while(++c<a)print c}{f($1)}END{f(d+1)}' d=$HSTA_REG_CNT $HSTA_UNIQ_TMP_FILE | \
awk '{printf("region%s 0.0\n", $1)}' >> $HSTA_UNIQ_TMP_FILE"_" 

sort -k2 -tn -n $HSTA_UNIQ_TMP_FILE"_" > $HSTA_UNIQ_TMP_FILE
## Amplifying the ptrace by 100 , otherwise the fluctuation of the $HSTA_UNIQ_TMP_FILE"_"erature is too small
awk '{printf("%s %s\n", $1, $2*100)}' $HSTA_UNIQ_TMP_FILE > $HSTA_UNIQ_TMP_FILE"_1" && mv $HSTA_UNIQ_TMP_FILE"_1" $HSTA_PTRACE_FILE_TEMP